﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 仿贴标机程序
{
    public partial class ChangeTemplateFrom : Form
    {
        public ChangeTemplateFrom()
        {
            InitializeComponent();

          //  textBox_customer.Text = Form1.value;
        }
        SQLServerDataBase sql = new SQLServerDataBase();
        private void button1_Click(object sender, EventArgs e)
        {
      


            if (textBox_upLabel.Text==""||textBox_heLabel.Text==""||textBox_boxLabel.Text=="")
            {
                MessageBox.Show("任何一项不得为空");
                return;
            }
            int result = 0;
            string ExistSql = "SELECT count(*) FROM jw_bartender WHERE custom_name = '"+ textBox_customer.Text+ "'";
  
            string insertSql = "INSERT INTO jw_bartender (custom_name,upLabel_name,heLabel_name,xiangLabel_name) VALUES ('"+ textBox_customer.Text +"','"+textBox_upLabel.Text+ "','" + textBox_heLabel.Text + "','"+textBox_boxLabel.Text+"')";

            string updateSql = "UPDATE jw_bartender SET upLabel_name = '" + textBox_upLabel.Text + "',heLabel_name = '" + textBox_upLabel.Text + "',xiangLabel_name = '" + textBox_boxLabel.Text + "' WHERE custom_name = '"+ textBox_customer.Text + "'";

            try
            {

                if (sql.ExecuteNonQuery(ExistSql) > 0)//有则更新，无则插入
                {
                    MessageBox.Show("" + sql.ExecuteNonQuery(updateSql, out result));
                }
                else
                {
                    MessageBox.Show("" + sql.ExecuteNonQuery(insertSql, out result));


                }



            }
            catch (Exception ex)
            {

                MessageBox.Show("result" + result +ex);
            }

            if (result==0)
            {
                MessageBox.Show("result" + "保存失败");

            }
            else
            {
                MessageBox.Show("result" + "保存成功");
            }
         



        }
        OpenFileDialog opdialog = new OpenFileDialog();
    
        DataSet ds = new DataSet();
        private void ChangeTemplateFrom_Load(object sender, EventArgs e)
        {
            
            textBox_customer.Text = Form1.value;
            try
            {
                sql.ExecuteNonDataSet("SELECT upLabel_name,heLabel_name,xiangLabel_name  FROM jw_bartender WHERE custom_name = '" + Form1.value + "'  ", out ds);
                textBox_upLabel.Text = ds.Tables[0].Rows[0][0].ToString();
                textBox_heLabel.Text = ds.Tables[0].Rows[0][1].ToString();
                textBox_boxLabel.Text = ds.Tables[0].Rows[0][2].ToString();

                textBox_upLabel.ReadOnly = true;
                textBox_heLabel.ReadOnly = true;
                textBox_boxLabel.ReadOnly = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(  "客户"+ Form1.value + ex.Message);
                return;
            }
           

        }

        private void textBox_heLabel_Click(object sender, EventArgs e)
        {
            opdialog.InitialDirectory = @"C:\Users";
            opdialog.Filter = "txt files (*.btw)|*.btw";
            opdialog.FilterIndex = 0;

            opdialog.RestoreDirectory = true;
            opdialog.ShowDialog();

            textBox_heLabel.Text = opdialog.FileName;

        }

        private void textBox_boxLabel_Click(object sender, EventArgs e)
        {

            opdialog.InitialDirectory = @"C:\Users";
            opdialog.Filter = "txt files (*.btw)|*.btw";
            opdialog.FilterIndex = 0;

            opdialog.RestoreDirectory = true;
            opdialog.ShowDialog();

            textBox_boxLabel.Text = opdialog.FileName;
        }

        private void textBox_upLabel_Click(object sender, EventArgs e)
        {

            opdialog.InitialDirectory = @"C:\Users";
            opdialog.Filter = "txt files (*.btw)|*.btw";
            opdialog.FilterIndex = 0;

            opdialog.RestoreDirectory = true;
            opdialog.ShowDialog();

            textBox_upLabel.Text = opdialog.FileName;
        }
    }
}
